package com.system.brandsserver.controller;

import com.system.brandsserver.common.Constant;
import com.system.brandsserver.common.ResultData;
import com.system.commonutils.bean.ApiResult;
import com.system.commonutils.bean.PagesBean;
import com.system.commonutils.pojo.GoodsBrand;
import com.system.brandsserver.service.BrandService;
import com.system.commonutils.pojo.GoodsType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ll
 * Date: 2020/07/07 9:23
 * Description:
 * Version: V1.0
 */
@RestController
@Api(value = "品牌管理",tags = "品牌管理-品牌种类API")
public class BrandController {
    /*
     * 这里需要注意的是，springcloud默认返回xml文件，这点不同于springboot，
     * 如果希望修改返回json格式，则需要在RequestMapping中添加produces = MediaType.APPLICATION_JSON_UTF8_VALUE
     */

    @Autowired
    BrandService brandService;

//    @Autowired
//    TestService testService;

    @RequestMapping(value = "/queryBrand",method = {RequestMethod.POST},
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "查询品牌种类接口",notes = "根据品牌种类id或者品牌类型名称模糊查询",httpMethod = "POST", response = ApiResult.class)
    public ApiResult queryBrand(@RequestBody GoodsBrand goodsBrand){
        PagesBean pagesBean = new PagesBean();
        System.out.println("controller" + goodsBrand + "======" + "显示条数：" + goodsBrand.getPageSize() + "=====" + "当前页码：" + goodsBrand.getNowsPage());
        pagesBean.setNowsPage(goodsBrand.getNowsPage());
        pagesBean.setPageSize(goodsBrand.getPageSize());
        List<GoodsBrand> list= brandService.queryBrand(goodsBrand);
        System.out.println("创建时间：" + list.get(0).getCreatetime());
        if(list.size() > 0){
            int totalSize = brandService.queryTotalSize(goodsBrand);
            System.out.println("总条数:" + totalSize);
            pagesBean.setDataCount(totalSize);
            pagesBean.setData(list);
            return ApiResult.successWithData("查询成功！", pagesBean);
        }else{
            return ApiResult.failure("查询失败！");
        }
    }



    @RequestMapping(value = "/delBrand",method = {RequestMethod.POST},
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "删除品牌种类接口",notes = "根据品牌id删除品牌",httpMethod = "POST", response = ApiResult.class)
    public ApiResult delBrand(@RequestBody GoodsBrand goodsBrand){
        int result = brandService.delBrand(goodsBrand);
//        return commonReturn(result);
        if(result > 0){
            return ApiResult.success("删除成功！");
        }else{
            return ApiResult.failure("删除失败！！");
        }
    }

    @RequestMapping(value = "/selectBrand",method = {RequestMethod.POST},
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "查询品牌详情接口",notes = "根据品牌种类id查询",httpMethod = "POST", response = ApiResult.class)
    public ApiResult selectBrand(@RequestBody GoodsBrand goodsBrand){
        List<GoodsBrand> list=  brandService.selectBrand(goodsBrand);
        if(list.size() > 0){
            return ApiResult.successWithData("查询成功！", list);
        }else{
            return ApiResult.failure("查询失败！请检查是否存在该类型！");
        }

}


    @RequestMapping(value = "/addBrand",method = {RequestMethod.POST},
            produces = "application/json;charset=utf-8")
    @ApiOperation(value = "添加商品种类接口",notes = "添加新的商品类型以及备注",httpMethod = "POST", response = ResultData.class)
    public ResultData addBrand(@RequestBody GoodsBrand goodsBrand){
        ResultData resultData = new ResultData();
        if(goodsBrand.getGoodsBrand_name()!=null&&goodsBrand.getGoodsBrand_logo()!=null&&goodsBrand.getRemarks()!=null
        &&!goodsBrand.getGoodsBrand_name().equals("string")&&!goodsBrand.getGoodsBrand_logo().equals("string")&&!goodsBrand.getRemarks().equals("string")){
            System.out.println("controller:" + "goodsBrand_name:" + goodsBrand.getGoodsBrand_name() + "===" + "remarks:" + goodsBrand.getRemarks());
            //查询是否已存在
            List<GoodsBrand> list = brandService.isExist(goodsBrand.getGoodsBrand_name());
            if(list.size() == 0){
                int result = brandService.addBrand(goodsBrand);
                if(result > 0){
                    resultData.setCode(Constant.CODE_STATUS_SUCCESS);
                    return resultData.setMessage("添加成功！");
                }else{
                    resultData.setCode(Constant.CODE_STATUS_FAILURE);
                    return resultData.setMessage("添加失败！");
                }
            }else{ resultData.setCode(Constant.CODE_STATUS_FAILURE);
                return resultData.setMessage("存在重复类型，添加失败！");
            }
        }else{
            return resultData.setMessage("添加失败！");
        }



    }

    @RequestMapping(value = "/editBrand",method = {RequestMethod.POST},
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "编辑品牌种类接口",notes = "根据品牌id编辑商品类型以及备注",httpMethod = "POST", response = ResultData.class)
    public ResultData editBrand (@RequestBody GoodsBrand goodsBrand )
    {
        ResultData resultData = new ResultData();

        if(!goodsBrand.getGoodsBrand_name().equals("")&&!goodsBrand.getGoodsBrand_logo().equals("")
                &&!goodsBrand.getGoodsBrand_name().equals("string")&&!goodsBrand.getGoodsBrand_logo().equals("string")){

            int result = brandService.editBrand(goodsBrand);
    //        return commonReturn(result);
            if(result > 0){
                resultData.setCode(Constant.CODE_STATUS_SUCCESS);
                return resultData.setMessage("编辑成功！");
            }else{
                resultData.setCode(Constant.CODE_STATUS_FAILURE);
                resultData.setMessage("编辑失败！存在重复商品类型！");
                return resultData;
            }
        }else{
            resultData.setCode(Constant.CODE_STATUS_FAILURE);
            System.out.println("=============failure================");
            return resultData.setMessage("编辑失败！！");
    }
}}




